package cn.jxau.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.ibatis.reflection.wrapper.BaseWrapper;
import org.aspectj.bridge.MessageWriter;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;

import cn.jxau.bean.po.Article;
import cn.jxau.bean.po.ArticleType;
import cn.jxau.bean.po.User;
import cn.jxau.bean.vo.ArticlePlus;
import cn.jxau.service.ArticleService;
import oracle.net.aso.a;

/**
 * 和文章相关的服务
 * @author lpy
 *
 */
@Service
public class ArticleServiceImpl extends AbstractCommonService implements ArticleService {

	@Override
	public List<Article> getAllArticle() {
		List<Article> list = articleMapper.selectList(null);
		return isShowArticle(list);
	}
	
	@Override
	public List<Article> getArticleListByPage(Page<Article> page) {
		List<Article> list=articleMapper.selectPage(page,new EntityWrapper<Article>().orderBy("id"));
		return isShowArticle(list);
	}

	@Override
	public List<Article> getArticleByTypeID(Page<Article> page,Long typeID) {
		List<Article> list = articleMapper.selectPage(page,new EntityWrapper<Article>().eq("typeID", typeID));
		return isShowArticle(list);
	}
	
	public List<Article> isShowArticle(List<Article> list){
		List<Article> articles = new ArrayList<>();
		for (Article article : list) {
			if(article.getExist()&&!article.getDraft()&&article.getQualified()){
				articles.add(article);
			}
		}
		return articles;
	}

	@Override
	public List<Article> getArticleByParentID(Page<Article> page, Long parentID) {
		return articleMapper.getArticleByParentID(page, parentID);
	}


	@Override
	public Article getArticleDetail(Long id) {
		return articleMapper.selectById(id);
	}

	@Override
	public User getAuthor(Long id) {
		return userMapper.selectById(id);
	}

	@Override
	public List<Article> getFiveArticle(Long authorID) {
		return articleMapper.getFiveArticle(authorID);
	}

	@Override
	public int insertArticle(Article article) {
		return articleMapper.insert(article);
	}

	@Override
	public List<ArticlePlus> getUserArticleList(Page<Article> page, Long userID) {
		List<Article> articles = articleMapper.selectPage(page,new EntityWrapper<Article>().eq("authorID", (long)userID));
		List<String> typeNames = articleTypeMapper.getUserArticleTypeName(page,userID);
		Integer rootComment = 0;
		Integer sonComment = 0;
		Integer sumComment = 0;
		Integer collectionCount = 0;
		List<ArticlePlus> userArticles = new ArrayList<>();
		
		for (int i=0;i<articles.size();i++) {
			rootComment = articleCommentMapper.getRootCommentCount(articles.get(i).getId());
			sonComment = articleCommentMapper.selectSonComment(articles.get(i).getId());
			sumComment = rootComment + sonComment;
			collectionCount = userCollectionsMapper.getCollectionCount(articles.get(i).getId());
			
			ArticlePlus articlePlus = new ArticlePlus();
			
			articlePlus.setArticleID(articles.get(i).getId());
			articlePlus.setTitle(articles.get(i).getTitle());
			articlePlus.setTdate(articles.get(i).getTdate());
			articlePlus.setIntroduction(articles.get(i).getIntroduction());
			articlePlus.setCommentCount(sumComment);
			articlePlus.setCollectionCount(collectionCount);
			articlePlus.setQualified(articles.get(i).getQualified());
			
			userArticles.add(articlePlus);
		}
		for (int i=0;i<typeNames.size();i++) {
			userArticles.get(i).setTypeName(typeNames.get(i));
		}
		return userArticles;
	}

	@Override
	public int deleteUserArticle(Long articleID) {
		return articleMapper.deleteById(articleID);
	}

	@Override
	public List<Article> getAllArticleInfo(Page<Article> page) {
		return articleMapper.getAllArticleInfo(page);
	}

	@Override
	public boolean updateArticleStatus(Article article) {
		boolean flag=false;
		Article art=articleMapper.selectById(article.getId());
		if(art!=null){
			flag=articleMapper.updateById(article)>0 ?true :false;
		}
		return flag;
	}
}




















